Two practical tasks - orderes and prices¶
Task #1¶
Write a program, which returns a list with 2-tuples.
Each tuple consists of a the order number and the product of the price per items and the quantity.
The product should be increased by 10, if the value of the order is less than 100,00.
Write a Python program using lambda and map.
L = [
[34587, 'Learning Python, Mark Lutz', 4, 40.95],
[98762, 'Programming Python, Mark Lutz', 5, 56.80],
[77226, 'Head First Python, Paul Barry', 3, 32.95],
[88112, 'Einfhrung in Python3, Bernd Klein', 3, 24.99],
]
A = []
L1 = []
for i in range(len(L)):
L1.append(L[i][0])
A.append(tuple(L1))
L2 = []
for i in range(len(L)):
L2.append(L[i][2] * L[i][3])
A.append(tuple(map(lambda x: x + 10 if x < 100.0 else x, L2)))
print(A) # [(34587, 98762, 77226, 88112), (163.8, 284.0, 108.85000000000001, 84.97)]
A = []
A.append(tuple(L[i][0] for i in range(len(L))))
A.append(tuple(map(lambda x: x + 10 if x < 100.0 else x, [L[i][2] * L[i][3] for i in range(len(L))])))
print(A) # [(34587, 98762, 77226, 88112), (163.8, 284.0, 108.85000000000001, 84.97)]
# print([*map(lambda x: (x[0], x[2]*x[3]), L)])
# [(34587, 163.8), (98762, 284.0), (77226, 98.85000000000001), (88112, 74.97)]
# ** Final statement **
min_order = 100
invoice_totals = list(map(lambda x: x if x[1] > min_order else (x[0], x[1] + 10), map(lambda x: (x[0], x[2]*x[3]), L)))
print(invoice_totals)
Output:
[(34587, 163.8), (98762, 284.0), (77226, 108.85000000000001), (88112, 84.97)]
Task #2¶
Write a program which returns a list of two tuples with (order number, total amount of order).
# ordernumber, (article number, quantity, price per unit),...
L = [
[1, ("5464", 4, 9.99), ("8274",18,12.99), ("9744", 9, 44.95)],
[2, ("5464", 9, 9.99), ("9744", 9, 44.95)],
[3, ("5464", 9, 9.99), ("88112", 11, 24.99)],
[4, ("8732", 7, 11.99), ("7733",11,18.99), ("88112", 5, 39.95)]
]
import reduce
# step #1
invoice_totals = list(map(lambda x: [x[0]] + list(map(lambda y: y[1]*y[2], x[1:])), L))
print(invoice_totals)
# [[1, 39.96, 233.82, 404.55], [2, 89.91, 404.55], [3, 89.91, 274.89], [4, 83.93, 208.89, 199.75]]
# step #2
invoice_totals = list(map(lambda x: [x[0]] + [reduce(lambda a, b: a + b, x[1:])], invoice_totals))
print(invoice_totals)
# [[1, 678.3299999999999], [2, 494.46000000000004], [3, 364.79999999999995], [4, 492.57]]
# ** Final statement **
min_order = 100
invoice_totals = list(map(lambda x: x if x[1] >= min_order else (x[0], x[1] + 10), invoice_totals))
print(invoice_totals)
Output:
[[1, 678.3299999999999], [2, 494.46000000000004], [3, 364.79999999999995], [4, 492.57]]